package com.ipzoe.web.controller.mem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.mem.model.MemberCardAddBatchDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.mem.domain.MemberCard;
import com.ipzoe.mem.service.IMemberCardService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 会员卡Controller
 * 
 * @author eric
 * @date 2020-08-20
 */
@RestController
@RequestMapping("/member/memberCard" )
public class MemberCardController extends BaseController {

    @Autowired
    private IMemberCardService iMemberCardService;

    /**
     * 查询会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberCard memberCard)
    {
        startPage();
        LambdaQueryWrapper<MemberCard> lqw = new LambdaQueryWrapper<MemberCard>();
        if (memberCard.getDealerId() != null){
            lqw.eq(MemberCard::getDealerId ,memberCard.getDealerId());
        }
        if (memberCard.getExpiryDay() != null){
            lqw.eq(MemberCard::getExpiryDay ,memberCard.getExpiryDay());
        }
        if (memberCard.getStatus() != null){
            lqw.eq(MemberCard::getStatus ,memberCard.getStatus());
        }
        lqw.orderByDesc(MemberCard::getId);
        List<MemberCard> list = iMemberCardService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:export')" )
    @Log(title = "会员卡" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MemberCard memberCard) {
        LambdaQueryWrapper<MemberCard> lqw = new LambdaQueryWrapper<MemberCard>(memberCard);
        List<MemberCard> list = iMemberCardService.list(lqw);
        ExcelUtil<MemberCard> util = new ExcelUtil<MemberCard>(MemberCard. class);
        return util.exportExcel(list, "memberCard" );
    }

    /**
     * 获取会员卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMemberCardService.getById(id));
    }

    /**
     * 新增会员卡
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:add')" )
    @Log(title = "会员卡" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberCard memberCard) {
        return toAjax(iMemberCardService.save(memberCard) ? 1 : 0);
    }

    /**
     * 批量创建会员卡
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:add')" )
    @Log(title = "会员卡" , businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addbatch(@RequestBody MemberCardAddBatchDTO memberCardAddBatchDTO) {
        iMemberCardService.generateBatch(memberCardAddBatchDTO);
        return toAjax(1);
    }

    /**
     * 修改会员卡
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:edit')" )
    @Log(title = "会员卡" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberCard memberCard) {
        return toAjax(iMemberCardService.updateById(memberCard) ? 1 : 0);
    }

    /**
     * 删除会员卡
     */
    @PreAuthorize("@ss.hasPermi('member:memberCard:remove')" )
    @Log(title = "会员卡" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMemberCardService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
